package com.fjp.heartguard.ui.message

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fjp.heartguard.app.Constants
import com.fjp.heartguard.app.base.BaseActivity
import com.fjp.heartguard.app.data.bean.Message
import com.fjp.heartguard.app.ext.init
import com.fjp.heartguard.databinding.ActivityMessageBinding
import com.fjp.heartguard.ui.message.adapter.MessageAdapter
import com.fjp.heartguard.viewmodel.MessageViewModel
import me.hgj.jetpackmvvm.ext.parseState

class MessageActivity : BaseActivity<MessageViewModel, ActivityMessageBinding>() {
    lateinit var mesageAdapter: MessageAdapter
    var messages: ArrayList<Message> = arrayListOf()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mViewModel.title.set("消息通知")
        mesageAdapter = MessageAdapter(messages) {
            val intent = Intent(this, MessageDetailActivity::class.java)
            intent.putExtra(Constants.INTENT_MESSAGE, it)
            startActivity(intent)
        }
        mViewModel.getMessageList()
        mDatabind.recyclerView.init(LinearLayoutManager(this, RecyclerView.VERTICAL, false), mesageAdapter)
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getMessageList()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.messageListResult.observe(this) {
            parseState(it, {
                messages = it
                if (messages.size == 0) {
                    mDatabind.emptyLl.visibility = View.VISIBLE
                } else {
                    mDatabind.emptyLl.visibility = View.GONE
                }
                mesageAdapter.setList(messages)
            }, {
                it.printStackTrace()
            })
        }
    }

}